package cn.aposoft.mq.kafka.admin.topic;

import org.apache.kafka.clients.admin.Admin;
import org.apache.kafka.clients.admin.ListOffsetsResult;
import org.apache.kafka.clients.admin.OffsetSpec;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.common.KafkaFuture;
import org.apache.kafka.common.TopicPartition;

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.ExecutionException;

public class ListOffsets {
    public static void main(String[] args) {
        Properties p = new Properties();
        p.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "aposoft.cn:10091,aposoft.cn:10092,aposoft.cn:10093");// kafka地址，多个地址用逗号分割
        try (Admin admin = Admin.create(p);) {
            Map<TopicPartition, OffsetSpec> topicPartitionOffsets = new HashMap<>();
            TopicPartition partition = new TopicPartition("test-new", 0);
            OffsetSpec offset = OffsetSpec.latest();
            topicPartitionOffsets.put(partition, offset);
            ListOffsetsResult result = admin.listOffsets(topicPartitionOffsets);
            KafkaFuture<Map<TopicPartition, ListOffsetsResult.ListOffsetsResultInfo>> future = result.all();
            System.out.println(future.get());
        } catch (ExecutionException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
